﻿// --------------------------------------------------------------------------------------------------------------------- 
// <copyright file="Problem3.cs" company="Microsoft">
//   Copyright (c) Microsoft Corporation.  All rights reserved. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// </copyright>
// <summary>
//   Defines the Problem3 type.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------
namespace EulerProblemsCSharp.Problems
{
    using System;
    using System.Linq;

    /// <summary>
    /// Problem 3.
    /// </summary>
    public class Problem3 : ProblemBase
    {
        /// <summary>
        /// Gets Number.
        /// </summary>
        /// <value>
        /// The number.
        /// </value>
        public override int Number
        {
            get
            {
                return 3;
            }
        }

        /// <summary>
        /// Gets Description.
        /// </summary>
        /// <value>
        /// The description.
        /// </value>
        public override string Description
        {
            get
            {
                return "What is the largest prime factor of the number 600851475143.";
            }
        }

        /// <summary>
        /// Solves problem 3.
        /// </summary>
        /// <returns>
        /// The solution.
        /// </returns>
        public override object Solve()
        {
            var query = from prime in new SieveOfEratosthenes().GetPrimes((int)Math.Sqrt(600851475143))                        
                        orderby prime descending
                        where 600851475143 % prime == 0
                        select prime;

            var answer = query.First();

            return answer;
        }
    }
}